package com.gsoe.problem.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.gsoe.base.exception.ProblemException;
import com.gsoe.problem.mapper.ProblemAnswerMapper;
import com.gsoe.problem.mapper.ProblemMapper;
import com.gsoe.problem.po.Problem;
import com.gsoe.problem.po.ProblemAnswer;
import com.gsoe.problem.service.ProblemAnswerService;
import com.gsoe.problem.vo.JudgeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 题目答案表 服务实现类
 * </p>
 *
 * @author xiaoma
 * @since 2024-10-27
 */
@Service
public class ProblemAnswerServiceImpl extends ServiceImpl<ProblemAnswerMapper, ProblemAnswer> implements ProblemAnswerService {
    @Autowired
    private ProblemMapper problemMapper;

    @Override
    public JudgeVO checkProblemAnswer(Long problemId, String answer) {
        Problem problem = problemMapper.selectById(problemId);
        if(problem==null){
            throw new ProblemException("未找到该题");
        }
        if(!problem.isVisible()){
            StpUtil.checkDisable("problem_unvisible_look");
        }

        JudgeVO judgeVO = new JudgeVO();
        judgeVO.setAnalysis(problem.getAnalysis());
        List<String> answerList = list(new QueryWrapper<ProblemAnswer>()
                .eq("problem_id", problemId)
        ).stream().map(ProblemAnswer::getAnswer).toList();
        judgeVO.setIsRight(answerList.contains(answer));
        judgeVO.setAnswer(answerList);
        judgeVO.setProblemDescribe(problem.getProblemDescribe());
        return judgeVO;
    }
}
